package cn.hinglo.gameclaw.alipay.action;

import cn.hinglo.gameclaw.alipay.service.LogisticsOrderService;
import cn.hinglo.gameclaw.data.annotation.IgnoreToken;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.CommonUtils;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @createTime: 2018/5/31
 * @author: HingLo
 * @description: 物流订单信息下单与校验
 */
@RequestMapping("/pay/l/")
@RestController
@Slf4j
public class LogisticsOrderAction extends BaseController {

    private final LogisticsOrderService logisticsOrderService;


    @Autowired
    public LogisticsOrderAction(LogisticsOrderService logisticsOrderService) {
        this.logisticsOrderService = logisticsOrderService;
    }

    /**
     * @param id        抓取记录集合的Id使用逗号隔开,例如：“12345,124,125"
     * @param addressId 邮寄的地址的唯一编号
     * @param type      类型 1:支付宝，2:微信
     * @return
     */
    @PostMapping("order")
    public Result order(String id, Long addressId, int type) {
        String userId = getUserId(request);
        String orderInfo;
        // 通过充值订单的Id来查询充值信息
        log.info("统一下单->用户：{},抓取记录：{},邮寄地址：{},支付类型：{}", userId, id, addressId, type);
        if (type == FieldUtils.PAYMENT_1) {
            //支付宝下单支付
            orderInfo = logisticsOrderService.logisticsAlipayOrderService(id, userId, addressId);
        } else {
            //表示微信下单支付
            orderInfo = logisticsOrderService.logisticsWeixinOrderService(id, userId, addressId, CommonUtils.getRemortIP(request));
        }
        log.info("订单下单结果：{}");
        if (orderInfo == null) {
            log.info("订单下单结果：{}", "下单失败");
            return error("订单信息获取失败");
        } else {
            log.info("订单下单结果：{}", "下单成功");
            return success(orderInfo);
        }

    }

    /**
     * 支付宝充值支付结果校验
     *
     * @return 是否处理成功
     */
    @PostMapping("moneyNotifyAlipay")
    @IgnoreToken
    public Result notifyAlipayMoneyOrder() {
        log.info("支付宝邮费支付结果通知");
        //验证是否支付失败
        return result(logisticsOrderService.logisticsAlipayNotifyService(request), "处理失败");
    }

    /**
     * 微信充值支付结果通过
     *
     * @return 是否成功
     */
    @PostMapping("moneyNotifyWeixin")
    public Result notifyWeixinMoneyOrder() {
        //验证是否支付失败
        log.info("微信邮费支付结果通知");
        return result(logisticsOrderService.logisticsWeixinNotifyService(request), "处理失败");
    }

    /***
     * 检测发货的娃娃是否包邮
     * @param id 抓中记录的Id集合
     * @return 是否包邮
     */
    @GetMapping("check")
    public Result check(String id) {
        return success(checks(id));
    }

    /**
     * 免邮下单
     *
     * @param id        Id
     * @param addressId 地址
     * @return 返回值
     */
    @PostMapping("free")
    public Result order(String id, Long addressId) {
        String userId = getUserId(request);
        if (checks(id)) {
            return result(logisticsOrderService.notFreeService(id, userId, addressId), "下单失败");
        } else {
            return error("发货数量不支持包邮哦");
        }
    }

    /**
     * 检测发货数量是否包邮
     *
     * @param id 需要发货的抓取记录id集合
     * @return true: 包邮 false: 不包邮
     */
    private boolean checks(String id) {
        if (StrUtil.isNotBlank(id)) {
            String[] ids = id.split(",");
            return ids.length >= FieldUtils.POSTAGE_NUM;
        } else {
            throw new RuntimeException("请求参数异常");
        }
    }
}
